/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UUGAME_DATA_REL_SQLCONNECTION_H_
#define UUGAME_DATA_REL_SQLCONNECTION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <vector>
#include <string>
#include <UGame/Macros.h>
#include <UGame/System/Mutex.h>

namespace UGame {
namespace Data {
namespace Rel {
    class QueryResult;
    class SqlStmtParameters;
    class SqlPreparedStatement;
    class Session;

    class SqlConnection
    {
    public:
        SqlConnection(Session* _Sess);
        virtual ~SqlConnection(void);

    public:
        virtual bool Open(const std::string& _Host,
            const std::string& _Port, const std::string& _User,
            const std::string _Password, const std::string& _Db) = 0;
        virtual void Close(void) = 0;
        virtual void EscapeString(std::string& _Sql) {}
        virtual void Ping(void) = 0;

        virtual std::string GetConnectionInfo(void) { return std::string(); }
        virtual std::string GetServerInfo(void) { return std::string(); }
        virtual std::string GetClientInfo(void) { return std::string(); }

        virtual QueryResult *Query(const std::string &_Sql) = 0;
        virtual bool Execute(const std::string &_Sql) = 0;
        virtual bool BeginTransaction(void) = 0;
        virtual bool CommitTransaction(void) = 0;
        virtual bool RollbackTransaction(void) = 0;

        SqlPreparedStatement* CreateStatement(const std::string& _Fmt);
        SqlPreparedStatement* GetStatement(std::uint32_t _Index);
        bool ExecuteStmt(int _Index, const SqlStmtParameters& _ID);

        bool IsError(void) const { return _error; }
        int GetErrorCode(void) const { return _errCode; }
        const std::string &GetErrorString(void) const { return _errString; }
        Session& session(void) const { return *_sess; }

        class Lock
        {
        public:
            Lock(SqlConnection* _Conn) : _conn(_Conn) { _conn->_lock.Lock(); }
            ~Lock() { _conn->_lock.Unlock(); }
            SqlConnection* operator->() const { return _conn; }

        private:
            SqlConnection* const _conn;
        };

    protected:
        void _ReportError(void);
        void _Uninitialize(void);

        typedef std::vector<SqlPreparedStatement*> StmtHolder;

        bool _error;
        int _errCode;
        std::string _errString;
        Session* _sess;
        System::Mutex _lock;
        StmtHolder _stmtHolder;

    private:
        DISALLOW_COPY_AND_ASSIGN(SqlConnection);
    };

}}}

#endif // UUGAME_DATA_REL_SQLCONNECTION_H_
